import {
  getAgentPage,
  deleteAgent,
  getAllTerminal
} from '../../../services/api/menber'
import Toast from 'tdesign-miniprogram/toast/index';

Page({
  /** 
   * 页面的初始数据 
   */
  data: {
    goodsListLoadStatus: 0,
    pageLoading: false,
    isActive: null,
    layout: 0,
    sorts: '',
    overall: 1,
    show: false,
    searchName: "",
    minVal: '',
    maxVal: '',
    saleFlag: "",
    type: "",
    hasLoaded: false,
    loadMoreStatus: 0,
    loading: true,
    listTitles: [],
    fixedTitle: null,
    toView: 'inToView0',
    oHeight: [],
    scroolHeight: 0,
    userList: [],
    agentUserId: '',
    pullDownRefreshing: false,
    emptyImg: 'https://cdn-we-retail.ym.tencent.com/miniapp/order/empty-order-list.png',
  },
  goodListPagination: {
    pageNo: 1,
    pageSize: 10,
  },
  submit() {
    this.loadGoodsList(true)
  },
  clear() {
    this.setData({
      searchName: ''
    }, () => {
      this.loadGoodsList()
    })
  },

  showFilterPopup() {
    this.setData({
      show: true,
    });
  },
  add() {
    wx.navigateTo({
      url: `/subpages/pages/addNewTerminal/addNewTerminal?agentUserId=${this.data.agentUserId}`,
    })
  },
  onLoad: function (options) {
    var that = this;
    var pages = getCurrentPages() //获取加载的页面
    var currentPage = pages[pages.length - 1] //获取当前页面的对象
    var options = currentPage.options //如果要获取url中所带的参数可以查
    let agentUserId = options.agentUserId;
    if (options.title) {
      wx.setNavigationBarTitle({
        title: options.title,
      });
      this.setData({
        type: options.type
      })
    }
    this.setData({
      agentUserId
    })
    this.loadHomePage();
    // that.getList();
  },
  toDetail(e) {
    wx.navigateTo({
      url: `/subpages/pages/addNewTerminal/addNewTerminal?isEdit=true&kid=${e.currentTarget.dataset.group.kid}&agentUserId=${this.data.agentUserId}`,
    })
  },


  async getList(id) {
    // const role = wx.getStorageSync('role');
    // const userId = role?.userId;
    let saleFlag;
    if (this.data.type == 1) {
      saleFlag = 0;
    } else if (this.data.type == 2) {
      saleFlag = 1;
    }
    this.setData({
      saleFlag
    })

    let {
      data
    } = await getAllTerminal({
      pageNo: 1,
      pageSize: 10,
      param: {
        agentUserId: this.data.agentUserId,
        saleFlag,
        customerName: this.data.searchName,
        phone: ''
      }
    });
    this.setData({
      userList: data.entities
    })
  },
  navToSaleOrder(e) {
    console.log(e.currentTarget.dataset.group);
    let group = e.currentTarget.dataset.group;
    let agentId = group.agentUserId;
    let customerId = group.kid
    wx.navigateTo({
      url: `/subpages/pages/saleOrder/saleOrder?orderId=${agentId}&customerId=${customerId}`,
    })
  },

  loadHomePage() {
    wx.stopPullDownRefresh();
    this.setData({
      pageLoading: true,
    });
    this.loadGoodsList(true);
  },
  onPullDownRefresh() {
    this.loadHomePage();
  },
  onReTry() {
    this.loadGoodsList();
  },
  async loadGoodsList(fresh = false) {
    const role = wx.getStorageSync('role');
    const userId = role?.userId;
    if (fresh) {
      wx.pageScrollTo({
        scrollTop: 0,
      });
      this.setData({
        pageLoading: false,
      });
    }
    this.setData({
      goodsListLoadStatus: 1
    });
    const pageSize = this.goodListPagination.pageSize;
    let pageIndex = this.goodListPagination.pageNo + 1;
    if (fresh) {
      pageIndex = 1;
    }
    try {
      const {
        data
      } = await getAllTerminal({
        "pageNo": pageIndex,
        "pageSize": pageSize,
        "param": {
          agentUserId: this.data.agentUserId,
          saleFlag: this.data.saleFlag,
          customerName: this.data.searchName,
          phone: ''
        }
      });
      this.setData({
        userList: fresh ? data.entities : this.data.userList.concat(data.entities),
        // goodsListLoadStatus: data.entities.length > 1 ? 0 : 2,
        goodsListLoadStatus: pageIndex == 1 && data.entities.length < 10 ? 2 : data.entities.length > 1 ? 0 : 2,
      });

      this.goodListPagination.pageSize = data.pageSize;
      this.goodListPagination.pageNo = data.pageNo;
    } catch (err) {
      this.setData({
        goodsListLoadStatus: 3
      });
    }
  },

  onReachBottom() {
    if (this.data.goodsListLoadStatus === 0) {
      this.loadGoodsList();
    }
  },



})